package com.wclass.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wclass.entity.CouresOrder;
import com.wclass.entity.CouresProgressBill;
import com.wclass.entity.CourseChapter;
import com.wclass.entity.CourseInfo;
import com.wclass.mapper.CouresProgressBillMapper;
import com.wclass.mapper.CourseChapterMapper;
import com.wclass.mapper.CourseInfoMapper;
import com.wclass.service.ICouresOrderService;
import com.wclass.service.ICouresProgressBillService;
import com.wclass.service.ICourseChapterService;
import com.wclass.service.ICourseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程学习情况 服务实现类
 * </p>
 *
 * @author 张浩高
 * @since 2018-12-20
 */
@Service
public class CouresProgressBillServiceImpl extends ServiceImpl<CouresProgressBillMapper, CouresProgressBill> implements ICouresProgressBillService {

    @Autowired
    private ICourseChapterService iCourseChapterService;
    @Autowired
    private ICourseInfoService iCourseInfoService;
    @Autowired
    private ICouresOrderService iCouresOrderService;

    /**
     * 获取用户课程学习进度
     * @param userID
     * @param courseInfoId
     * @return
     */
    @Override
    public Map<String, Object> selectMapByUserIdAndCouresId(Long userID, Long courseInfoId) {
        CourseInfo courseInfo = iCourseInfoService.selectById(courseInfoId);
        EntityWrapper<CourseChapter> ew_chapter = new EntityWrapper<>();
        ew_chapter.eq("course_info_id", courseInfoId).ne("p_id", 0);
        Integer couresCount = iCourseChapterService.selectCount(ew_chapter);//当前课程总章节数量

        EntityWrapper<CouresProgressBill> ew_progress = new EntityWrapper<>();
        ew_progress.orderBy("crt_time");
        ew_progress.ne("state", 0);
        ew_progress.eq("user_id", userID).eq("course_info_id", courseInfoId);
        List<CouresProgressBill> list = baseMapper.selectList(ew_progress);
        Map<String, Object> map = Maps.newHashMap();
        map.put("courseInfo", courseInfo);
        map.put("couresCount", couresCount);
        map.put("readCount", list.size());
        map.put("lateTime", list != null && list.size() > 0 ? list.get(0).getCrtTime() : "--");
        return map;
    }

    /**
     * 章节学习时长，没有记录新增，存在记录，已学完状态不更新记录，未学完状态下本次学习时长大于记录中学习时长则更新记录
     * @param couresId
     * @param courseChapterId
     * @param userID
     * @param state
     * @param studyDuration
     */
    @Override
    public void insertCouresProgressBill(Long couresId, Long courseChapterId, Long userID, Integer state, Integer studyDuration) {
        EntityWrapper<CouresProgressBill> ew = new EntityWrapper<>();
        ew.eq("course_info_id", couresId).eq("course_chapter_id", courseChapterId).eq("user_id", userID);
        List<CouresProgressBill> list = baseMapper.selectList(ew);
        if (list.size() == 0) {
            CouresProgressBill info = new CouresProgressBill();
            info.setCrtTime(new Date());
            info.setUserId(userID);
            info.setCourseInfoId(couresId);
            info.setCourseChapterId(courseChapterId);
            info.setState(state);
            info.setStudyDuration(studyDuration);
            baseMapper.insert(info);
        } else {
            CouresProgressBill info = list.get(0);
            if (info.getState() == 1 && info.getStudyDuration() < studyDuration) {
                info.setState(state);
                info.setStudyDuration(studyDuration);
            }
            info.setCrtTime(new Date());
            baseMapper.updateById(info);
        }
    }

    /**
     * 判断学生课程学习进度，有未学完的就返回true
     * 否则返回false
     * @return
     */
    @Override
    public boolean judgeCourseStudentProgress(Long courseInfoId){
        EntityWrapper<CouresOrder> ew = new EntityWrapper<>();
        ew.eq("info.course_info_id", courseInfoId);
        ew.orderBy("info.crt_time", false);
        List<CouresOrder> list = iCouresOrderService.selectList(ew);

        for (CouresOrder order : list) {
            Map<String, Object> map = this.selectMapByUserIdAndCouresId(order.getPayPersonId(), courseInfoId);
            if (!map.get("couresCount").equals(map.get("readCount"))) {
                return true;
            }
        }

        return false;
    }

}
